package com.shareach.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObjectBuilder;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BizCardDao;
import com.shareach.domain.BizCardDomain;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

/**
 * create time：2013-1-31 上午11:31:44
 * 
 * @author retryu
 * @since JDK 1.6
 * @version 1.0 description：
 */
@Service
public class BizCardService extends BaseService<BizCardDomain> implements com.shareach.service.BizCardService {

	@Autowired
	private BizCardDao bizCardDao;

	@Override
	public synchronized Long save(BizCardDomain domain) {
		domain.setCreatTime(new Date());
		return bizCardDao.insert(domain);
	}

	@Override
	public void del(BizCardDomain domain) {
		if (domain.getId() == null && (domain.getSite() == null && domain.getSiteId() == null))
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		bizCardDao.delete(query, BizCardDomain.class);
	}

	@Override
	public synchronized void update(BizCardDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			bizCardDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public List<BizCardDomain> getList(BizCardDomain domain) {
		Query query = new Query();
		query = parseDomaintoQuery(domain);
		List<BizCardDomain> bizCardDomains = bizCardDao.findList(query);
		return bizCardDomains;
	}

	@Override
	public BizCardDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BizCardDomain bizCardDomain = bizCardDao.findOne(query);
		return bizCardDomain;
	}

	@Override
	public Page<BizCardDomain> getPageResult(BizCardDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BizCardDomain> domainPage = bizCardDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public synchronized void update(BizCardDomain domain, BizCardDomain condition) {
		Update update = parseDomaintoUpdate(domain);
		Query query = parseDomaintoQuery(condition);

		// if (condition.getId() != null) {
		// query.addCriteria(Criteria.where("_id").is(condition.getId()));
		// }
		// update tels if necessary
		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}

		if (update.getUpdateObject().toMap().size() != 0) {
			bizCardDao.update(query, update);
		}
	}

	@Override
	public BizCardDomain findOne(BizCardDomain domain) {
		Query query = parseDomaintoQuery(domain);
		BizCardDomain card = bizCardDao.findOne(query);
		return card;
	}

	@Override
	public synchronized Map<String, Object> saveOrUpdate(BizCardDomain domain) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			if (domain.getSite() == null || domain.getSiteId() == null) {
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
				return retMap;
			} else {
				// 存在site，siteid
				BizCardDomain paraDomain = new BizCardDomain();
				paraDomain.setSite(domain.getSite());
				paraDomain.setSiteId(domain.getSiteId());

				BizCardDomain retDomain = findOne(paraDomain);
				// 存在对应的团购,则更新<name&addr must equal>
				if (retDomain != null) {
					// 不更新指定字段
					domain.setName(null);
					domain.setAddr(null);
					update(domain, paraDomain);
					// return value
					retMap.put(Globals.GLOBAL_SERVICE_RET_ID, retDomain.getId());
					retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_EXIST);
					return retMap;
				}
				// 不存在对应的团购
				else {
					save(domain);
					retMap.put(Globals.GLOBAL_SERVICE_RET_ID, domain.getId());
					retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_NOTEXIST);
					return retMap;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
			return retMap;
		}
	}

}
